package cc.lj.internal.core.model;

import cn.hutool.core.date.DateUtil;
import lj.internal.dict.enums.EventSourceType;
import lombok.Data;

import java.util.Date;
import java.util.UUID;

/**
 * @Description: event通用返回对象体
 * @Author: liuguosheng
 * @Date: 2021-04-23 10:57:15
 */
@Data
public class BaseDomainEvent<T> {
    /**
     * 领域事件ID
     */
    private String id;
    /**
     * 领域事件时间戳
     */
    private Date timestamp;
    /**
     * 记录事件源
     */
    private EventSourceType source;
    /**
     * 事件业务数据
     */
    private T data;

    public static <T> BaseDomainEvent<T> createDomainEvent(T data, EventSourceType eventSourceType) {
        BaseDomainEvent<T> domainEvent = new BaseDomainEvent<>();
        domainEvent.setId(UUID.randomUUID().toString().replace("-", ""));
        domainEvent.setSource(eventSourceType);
        domainEvent.setTimestamp(DateUtil.date());
        domainEvent.setData(data);
        return domainEvent;
    }
}
